package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/advert/model"
	"time"
)

type AdvertMapper struct{}

/**
 * @desc 根据类型查询广告
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 * SELECT * FROM xk_advert WHERE `status` = 1 AND is_deleted = 0  AND type = 2 AND start_time <=NOW() AND end_time >=NOW() ORDER BY sorted ASC;
 */
func (service *AdvertMapper) FindAdvertByType(ftype int64, systemId uint64) []*model.Advert {
	// 1：准备容器对象，开始装载数据库数据
	advertList := []*model.Advert{}
	// 2: 创建一个数据连接
	mysql := orm.NewOrm()
	// 3: 指定查询的表
	qs := mysql.QueryTable("xk_advert")
	// 4: 创建查询条件
	whereCondition := orm.NewCondition()
	whereCondition = whereCondition.
		And("status", 1).
		And("is_deleted", 0).
		And("system_id", systemId).
		And("start_time__lte", time.Now()).
		And("end_time__gte", time.Now()).
		And("type", ftype)
	// 4: 执行查询
	_, err := qs.SetCond(whereCondition).OrderBy("sorted").All(&advertList)
	if nil != err {
		return nil
	}
	return advertList
}
